# -*- coding:utf-8 -*-
from __future__ import division
import pandas as pd
import numpy as np
import re

min_year_dict = {
    305: 4,
    -1:-1,
    1:1,
    103:2,
    0:0,
    510:7,
    1099:10,
    399:4,
    599:7,
    199:1,
    299:2,
    110:1
}

degree_dict = {
    '初中':1,'中技':2,'高中':3,'中专':3,
    '大专':4,'本科':5,
    '硕士':6,'博士':7,
    'EMBA':7,'MBA':6,
    '其他':0,'请选择':0,
    '\\N':0,'na':0
}

salary_dict = {
    1000:500,
    100002000:1500,
    200104000:3000,
    400106000:5000,
    600108000:7000,
    800110000:9000,
    100001150000:100001,
    1000115000:12500,
    1500120000:17500,
    1500125000:20000,
    2000130000:25000,
    2500199999:25001,
    3000150000:40000,
    3500150000:45000,
    5000170000:60000,
    70001100000:80000,
    2500135000:30000,
    0:0
}


def get_city_index(city_list,index_of_list):
    cities = city_list.split(',')
    if index_of_list < len(cities):
        if cities[index_of_list] == '-':
            return -1
        else:
            return int(cities[index_of_list])
    else:
        return -1


def get_city_cnt(city_list):
    cities = city_list.split(',')
    cnt = 0
    for city in cities:
        if city != '-':
            cnt += 1
    return cnt


def get_match_score(experience,job_describtion):
    experiences = []
    if str(job_describtion) is not 'nan':
        experiences = experience.split('|')
    num = 0
    for item in experiences:
        if item in job_describtion:
            num += 1
    return num / (len(experiences) + 1)


def is_number(var):
    if var >= '0' and var <= '9':
        return True
    else:
        return False

def get_set_match(user_desc, job_desc, pattern='\\|'):
    job_desc = str(job_desc)
    user_desc = str(user_desc)
    job_desc_set = set(re.split(pattern,user_desc))
    user_desc_set = set(re.split(pattern,user_desc))
    match_size = len(user_desc_set & job_desc_set)
    return match_size / (len(job_desc) + 1)


def generate_feature(data_frame):
    print('Generate Feature !')
    data_frame['desire_jd_city_1'] = data_frame['desire_jd_city_id'].apply(lambda city_list: get_city_index(city_list, 0))
    data_frame['desire_jd_city_2'] = data_frame['desire_jd_city_id'].apply(lambda city_list: get_city_index(city_list, 1))
    data_frame['desire_jd_city_3'] = data_frame['desire_jd_city_id'].apply(lambda city_list: get_city_index(city_list, 2))
    data_frame['desire_jd_city_cnt'] = data_frame['desire_jd_city_id'].apply(get_city_cnt)

    data_frame['city_eq_jd_city_1'] = data_frame['desire_jd_city_1'] == data_frame['city']
    data_frame['city_eq_jd_city_2'] = data_frame['desire_jd_city_2'] == data_frame['city']
    data_frame['city_eq_jd_city_3'] = data_frame['desire_jd_city_3'] == data_frame['city']

    data_frame['work_years'] = 2019 - data_frame['start_work_date'].apply(lambda x: 2018 if x == '-' else int(x))

    data_frame['desire_salary'] = data_frame['desire_jd_salary_id'].apply(lambda x: salary_dict[x])

    data_frame['min_years'] = data_frame.min_years.apply(lambda x: min_year_dict[x])

    data_frame['work_years_satisfied'] = data_frame['work_years'].astype(int) - data_frame['min_years']

    data_frame['salary_more_than_desire'] = data_frame['desire_salary'] <= data_frame['min_salary']
    data_frame['salary_less_than_desire'] = data_frame['desire_salary'] >= data_frame['max_salary']

    data_frame['cur_salary'] = data_frame['cur_salary_id'].apply(lambda x: salary_dict[int(x if is_number(x) else '0')])

    data_frame['salary_more_than_cur'] = data_frame['cur_salary'] > data_frame['min_salary']

    data_frame['dist_desire_and_curr_salary'] = data_frame['desire_salary'] - data_frame['cur_salary']
    data_frame['dist_desire_and_job_salary'] = data_frame['min_salary'] - data_frame['desire_salary']
    data_frame['dist_curr_and_job_salary'] = data_frame['min_salary'] - data_frame['cur_salary']

    data_frame['cur_degree_id'] = data_frame['cur_degree_id'].fillna('na').apply(lambda x: degree_dict[x.strip()])

    data_frame['experience_num'] = data_frame['experience'].apply(lambda x: len(str(x).split('|')) if str(x) != 'nan' else 0)

    data_frame['min_edu_level'] = data_frame['min_edu_level'].fillna('na').apply(lambda x: degree_dict[x.strip()])

    data_frame['degree_distance'] = data_frame['cur_degree_id'] - data_frame['min_edu_level']

    data_frame['job_match_score'] = data_frame.apply(lambda row:get_match_score(str(row['experience']),row['job_description']),axis=1)

    data_frame['desire_jd_type_score'] = data_frame.apply(lambda row: get_set_match(str(row['desire_jd_type_id']), row['jd_sub_type'], pattern='/|,'), axis=1)

    data_frame['curr_jd_type_score'] = data_frame.apply(lambda row: get_set_match(str(row['cur_jd_type']), row['jd_sub_type'], pattern='/|,'), axis=1)

    return data_frame